package com.Mms.service;

import com.Mms.Vo.LoginVo;
import com.Mms.Vo.ShowVo;
import com.Mms.Vo.Theam;
import com.Mms.dao.AnymessageMapper;
import com.Mms.dao.MinfoMapper;
import com.Mms.dao.MuserMapper;
import com.Mms.dao.UinfoMapper;
import com.Mms.model.*;
import com.mchange.v2.sql.filter.SynchronizedFilterResultSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class serviceBySelectImpl implements serviceBySelect {

    @Autowired
    private MuserMapper muserMapper;

    @Autowired
    private MinfoMapper minfoMapper;

    @Autowired
   private AnymessageMapper anymessageMapper;

    @Autowired
    private UinfoMapper uinfoMapper;



    @Override
    public List<ShowVo> selectByCheck(int flag) {
        List<Muser> muserList = new ArrayList<>();
        if(flag==1){
      MuserExample example = new MuserExample();
       // MuserExample.Criteria criteria = example.createCriteria();  使用方法类似于下面的代码结构
      example.createCriteria().andUboolEqualTo(0);
            muserList = muserMapper.selectByExample(example);
        }else {
            MuserExample example = new MuserExample();
            // MuserExample.Criteria criteria = example.createCriteria();  使用方法类似于下面的代码结构
            example.createCriteria().andUboolEqualTo(1);
            muserList = muserMapper.selectByExample(example);
        }
      List<ShowVo> showVoList = new ArrayList<>();
      for (int i=0;i<muserList.size();i++){
      ShowVo showVo = new ShowVo();
      Uinfo uinfo = uinfoMapper.selectByPrimaryKey(muserList.get(i).getUstuno());
      Minfo minfo = minfoMapper.selectByPrimaryKey(muserList.get(i).getMno());
      showVo.setMuser(muserList.get(i));
      showVo.setMinfo(minfo);
      showVo.setUinfo(uinfo);
      showVoList.add(showVo);
      }return showVoList;

    }

    /**
     *   返回值含义：loginVo 中的flag 值含义 0 用户名密码错误，1 用户名密码正确
     * @param ustuno
     * @param upassword
     * @return
     */
    @Override
    public LoginVo selectByLogin(String ustuno, String upassword) {
        LoginVo loginVo = new LoginVo();
        System.out.println("查询结束");
        MuserExample example = new MuserExample();
        MuserExample.Criteria criteria =example.createCriteria();
        criteria.andUstunoEqualTo(ustuno);
        criteria.andUpasswordEqualTo(upassword);
         List<Muser> muser = muserMapper.selectByExample(example);
        System.out.println("查询结束");
         if ( muser.size() == 0){
              loginVo.setFlag(0);
             return loginVo;
         }

        /**
         * 查询用户基本信息
         */
        loginVo.setFlag(1);
         loginVo.setMuser(muser.get(0));
          Minfo minfo =  minfoMapper.selectByPrimaryKey(muser.get(0).getMno());
          loginVo.setMinfo(minfo);
          //查询本社团的所有消息
        System.out.println("查询本社团所有消息");
        AnymessageExample anymessageExample = new AnymessageExample();
        AnymessageExample.Criteria anycriteria = anymessageExample.createCriteria();
        System.out.println(muser.get(0).getMno()); //输出社团编号
        anycriteria.andToorganEqualTo(muser.get(0).getMno());
       List<Anymessage> anymessageList = anymessageMapper.selectByExample(anymessageExample);
       loginVo.setAnymessage(anymessageList);

        //查询社联中所有活动的消息
        AnymessageExample anymessageExample1 = new AnymessageExample();
        AnymessageExample.Criteria anycriteria1 = anymessageExample1.createCriteria();
        anycriteria1.andTypeEqualTo(2);
        anycriteria1.andToorganEqualTo("1");
        List<Anymessage> anymessageList2 = anymessageMapper.selectByExample(anymessageExample1);
        System.out.println(anymessageList2.size()+"!!!!!");
        Theam theam = new Theam();
         theam.setFlag(1);
         theam.setAnymessages(anymessageList2);
         loginVo.setTheam(theam);


          return loginVo;
    }

    @Override
    public List<ShowVo> selectByall() {
        List<ShowVo> showVoList = new ArrayList<>();

      List<Muser> muserList = muserMapper.selectByall();
       for(int i=0;i<muserList.size();i++){
           ShowVo showVo = new ShowVo();
          Uinfo uinfo = uinfoMapper.selectByPrimaryKey(muserList.get(i).getUstuno());
          Minfo minfo = minfoMapper.selectByPrimaryKey(muserList.get(i).getMno());
           showVo.setUinfo(uinfo);
           showVo.setMinfo(minfo);
           showVo.setMuser(muserList.get(i));
           showVoList.add(showVo);
       }


        return showVoList;
    }

    @Override
    public List<ShowVo> selectByMno(String mno) {
        List<ShowVo> showVoList = new ArrayList<>();

        MuserExample example = new MuserExample();
        MuserExample.Criteria criteria = example.createCriteria();
        criteria.andMnoEqualTo(mno);
        List<Muser> muserList = muserMapper.selectByExample(example);
        for(int i=0;i<muserList.size();i++){
            ShowVo showVo = new ShowVo();
            Uinfo uinfo = uinfoMapper.selectByPrimaryKey(muserList.get(i).getUstuno());
            Minfo minfo = minfoMapper.selectByPrimaryKey(muserList.get(i).getMno());
            showVo.setUinfo(uinfo);
            showVo.setMinfo(minfo);
            showVo.setMuser(muserList.get(i));
            showVoList.add(showVo);
        }
        return showVoList;
    }
}
